/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusServiceRel;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class RemoteServiceBusBindingContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private ListBox bus;

	private String domainId;

	private ArrayList<BusServiceRel> busR;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code1 = res.getCode();
			if (!code1.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			// Service s = (Service)context.get(ServiceConstants.SERVICE);
			// s.setId((String)res.getResponse());
			HashMap<String, String> t = new HashMap<String, String>();
			for (BusServiceRel r : busR) {
				t.put(r.getBusId(), r.getBusId());
				// t.put();
			}

			ArrayList<KeyNamePairs> l = (ArrayList<KeyNamePairs>) res
					.getResponse();
			bus.clear();
			bus.setVisibleItemCount(l.size() == 0 ? 1 : l.size());
			for (int i = 0, j = l.size(); i < j; i++) {
				KeyNamePairs k = l.get(i);
				bus.addItem(k.getName(), k.getId());
				if (t.get(k.getId()) != null) {
					bus.setSelectedIndex(i);
				}
			}

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			Window.alert(cons.modifyOK());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RemoteServiceBusBindingContent(ServiceMessages cons,
			BifangGWTServiceAsync s, GwtContext c, String domainId,
			ArrayList<BusServiceRel> busR) {
		this.service = s;
		this.cons = cons;
		this.context = c;
		this.domainId = domainId;
		this.busR = busR;

		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		HTML h = new HTML(cons.bindingBus());
		panel.add(h);
		bus = new ListBox(true);
		bus.setWidth("11em");

		panel.add(bus);
		panel.add(new HTML("&nbsp;"));

		Button b1 = new Button(cons.modify());
		b1.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				String serviceId = null;
				if (busR.size() > 0) {
					BusServiceRel r = busR.get(0);
					serviceId = r.getServiceId();
				}

				ArrayList<BusServiceRel> tmp = new ArrayList<BusServiceRel>();
				for (int i = 0, j = bus.getItemCount(); i < j; i++) {
					if (bus.isItemSelected(i)) {
						BusServiceRel r = new BusServiceRel();
						r.setBusId(bus.getValue(i));
						r.setServiceId(serviceId);
						tmp.add(r);
					}
				}

				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyMappingServiceBusBindingAction");
				req.setRequest(busR);
				service.call(req, cb1);
			}

		});

		if (PrivilegChecker.hasPrivilege(context, null))
			panel.add(b1);
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.service.GetDomainBusListAction");

		req.setRequest(domainId);
		service.call(req, cb);

	}

}
